package com.fl.nj.service.impl;

import com.fl.nj.entity.TableSchema;
import com.fl.nj.mapper.TableSchemaMapper;
import com.fl.nj.service.TableSchemaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 表结构信息Service实现类
 * 实现表结构信息的业务逻辑
 */
@Service
public class TableSchemaServiceImpl implements TableSchemaService {
    @Autowired
    private TableSchemaMapper tableSchemaMapper;

    @Override
    @Transactional
    public void save(TableSchema schema) {
        // 验证表名是否已存在
        TableSchema existingSchema = tableSchemaMapper.getByDbConfigIdAndTableName(
                schema.getDbConfigId(), schema.getTableName());
        if (existingSchema != null) {
            throw new RuntimeException("表名已存在: " + schema.getTableName());
        }
        
        // 验证必填字段
        validateSchema(schema);
        
        // 保存表结构信息
        tableSchemaMapper.save(schema);
    }

    @Override
    @Transactional
    public void update(TableSchema schema) {
        // 验证表结构是否存在
        TableSchema existingSchema = tableSchemaMapper.getById(schema.getId());
        if (existingSchema == null) {
            throw new RuntimeException("表结构不存在");
        }
        
        // 验证表名是否已被其他表使用
        TableSchema schemaWithSameName = tableSchemaMapper.getByDbConfigIdAndTableName(
                schema.getDbConfigId(), schema.getTableName());
        if (schemaWithSameName != null && !schemaWithSameName.getId().equals(schema.getId())) {
            throw new RuntimeException("表名已存在: " + schema.getTableName());
        }
        
        // 验证必填字段
        validateSchema(schema);
        
        // 更新表结构信息
        tableSchemaMapper.update(schema);
    }

    @Override
    public TableSchema getById(Long id) {
        return tableSchemaMapper.getById(id);
    }

    @Override
    public TableSchema getByDbConfigIdAndTableName(Long dbConfigId, String tableName) {
        return tableSchemaMapper.getByDbConfigIdAndTableName(dbConfigId, tableName);
    }

    @Override
    public List<TableSchema> getByDbConfigId(Long dbConfigId) {
        return tableSchemaMapper.getByDbConfigId(dbConfigId);
    }

    @Override
    public List<TableSchema> getAll() {
        return tableSchemaMapper.getAll();
    }

    @Override
    @Transactional
    public void delete(Long id) {
        // 验证表结构是否存在
        TableSchema schema = tableSchemaMapper.getById(id);
        if (schema == null) {
            throw new RuntimeException("表结构不存在");
        }
        
        // 删除表结构信息
        tableSchemaMapper.delete(id);
    }

    /**
     * 验证表结构信息的必填字段
     * @param schema 表结构信息对象
     */
    private void validateSchema(TableSchema schema) {
        if (schema.getDbConfigId() == null) {
            throw new RuntimeException("数据库配置不能为空");
        }
        if (schema.getTableName() == null || schema.getTableName().trim().isEmpty()) {
            throw new RuntimeException("表名不能为空");
        }
    }
}